/*
 * @Author: wanglidong
 * @Date: 2022-02-08 15:47:53
 * @LastEditors: wanglidong
 * @LastEditTime: 2022-02-11 10:06:47
 * @Description: 开始你的表演
 * @FilePath: \lowcode-front\src\utils\publicFun.js
 */
/*  表头 颜色 */
export const tableRowClassNameGol=({row, rowIndex})=> {
  return 'tableHeaderRow';
}
/*  表格  row-style 颜色 */
export const tableRowStyleGol=({row, rowIndex})=> {
  return 'tableRowStyleCss';
}
/*  表格  cell-style 颜色 */
export const tableCellStyleGol=({row,column,rowIndex,columnIndex})=> {
  return 'tableCellStyleCss';
}
/*  表格  header-cell-style 颜色 */
export const tableHeaderCellStyleGol=({row,column,rowIndex,columnIndex})=> {
  return 'tableHeaderCellStyleCss';
}
/*  修改指定列颜色 */
export const setCellColorGol=({row,column,rowIndex,columnIndex})=>{

}
/*  wx列间隔 */
export const cellFormat = ({row, column, rowIndex, columnIndex}) =>{
  if(column) {
    return `padding: 8px 6px`
  }
}
/*  older wx列间隔 */
export const olderCellFormat = ({row, column, rowIndex, columnIndex}) =>{
  if(column) {
    return `padding: 8px 6px; font-size: 18px`
  }
}

//防抖debounce代码：
export function debounce(fn) {
  let timeout = null; // 创建一个标记用来存放定时器的返回值
  return (...arg) => {
    // 每当用户输入的时候把前一个 setTimeout clear 掉
    clearTimeout(timeout);
    // 然后又创建一个新的 setTimeout, 这样就能保证interval 间隔内如果时间持续触发，就不会执行 fn 函数
    timeout = setTimeout(() => {
      // @ts-ignore
      fn.apply(this, arg);
    }, 500);
  };
}
export function deffArr(tree,callback){
  let node, nodes = tree.slice()
  while(node = nodes.shift()){
    if (callback)callback(node)
    if(node.children && node.children.length){
      nodes.push(...node.children)
    }
  }
}
export function compareRowSpan(row, column, rowIndex, columnIndex, fields, list) {
  if (rowIndex > 0) {
    let bf = list[rowIndex-1]
    if (equal(bf, row, fields)) {
      return [0, 0]
    }
  }

  let rowspan = 1
  if (rowIndex < list.length-1) {
    let idx = rowIndex + 1
    let af = list[idx]
    while (equal(af, row, fields)) {
      ++rowspan
      ++ idx
      if (idx < list.length) {
        af = list[idx]
      } else {
        break
      }
    }
  }
  return [rowspan, 1]

  function equal(row1, row2, fields) {
    for(const i in fields) {
      const field = fields[i]
      if (row1[field] !== row2[field]) {
        return false
      }
    }

    return true
  }
}

export function doCreateObject(res, fileName) {
  let blob = new Blob([res],
    {
      type: 'application/octet-stream;charset=UTF-8'
    });
  let downloadElement = document.createElement('a');
  let href = window.URL.createObjectURL(blob); //创建下载的链接
  downloadElement.style.display = 'none';
  downloadElement.href = href;
  downloadElement.download = decodeURI(fileName); //下载后文件名
  document.body.appendChild(downloadElement);
  downloadElement.click(); //点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素
  window.URL.revokeObjectURL(href); //释放掉blob对象
}
